<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
            <el-breadcrumb-item :to="{ path: '/' }">bug管理</el-breadcrumb-item>
            <el-breadcrumb-item>新增bug</el-breadcrumb-item>
        </el-breadcrumb>

        <el-divider></el-divider>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="bug描述" prop="description">
                <el-input v-model="ruleForm.description" type="textarea" :rows="4" style="width: 80%;"/>
            </el-form-item>
            <el-form-item label="项目名称" prop="proId">
                <el-select v-model="ruleForm.proId" placeholder="请选择">
                    <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.proName"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="上传图片" prop="url">
                <el-upload
                        action="http://localhost:8989/upload"
                        name="pic"
                        :limit="1"
                        list-type="picture-card"
                        :on-success="handleSuccess"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                fileList: [],
                options: [],
                ruleForm: {
                    description: '',
                    proId:'',
                    url: ''
                },
                rules: {
                    description: [
                        {required: true, message: '请描述bug相关信息', trigger: 'blur'},
                        {min: 4, max: 500, message: '长度在 4 到 500 个字符', trigger: 'blur'}
                    ],
                    proId: [
                        {required: true, trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                this.axios
                    .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
                    .get('http://localhost:8989/remove?url='+file.response).then(function () {
                    console.log("删除服务器图片完成!")
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleSuccess(response){
                this.ruleForm.url = response;
            },
            loadProjectList() {
                console.log('loadProjectList');
                let url = 'http://localhost:8989/projects/list';
                console.log('url = ' + url);
                this.axios
                    .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
                    .get(url).then((response) => {
                    let responseBody = response.data;
                    this.options = responseBody.data;
                });
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let url = 'http://localhost:8989/bugs/add-new';
                        console.log('url = ' + url);
                        let formData = this.qs.stringify(this.ruleForm, {arrayFormat: 'repeat'});
                        console.log('formData = ' + formData);
                        this.axios
                            .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
                            .post(url, formData).then((response) => {
                            let responseBody = response.data;
                            console.log('responseBody = ');
                            console.log(responseBody);
                            if (responseBody.state == 20000) {
                                this.$message({
                                    message: '提交bug成功！',
                                    type: 'success'
                                });
                                this.resetForm(formName);
                            } else {
                                this.$message.error(responseBody.message);
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        mounted() {
            this.loadProjectList();
        }
    }
</script>